package com.wyz.module.config.security.controller;

import com.wyz.module.config.security.model.dto.LoginUserDTO;
import com.wyz.module.config.security.service.MyDbUserDetailsManager;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author 无涯子
 * @date 2024/4/20
 */
@RestController
@RequestMapping("/login_user")
public class LoginUserController {
    @Resource
    private MyDbUserDetailsManager manager;


    @PostMapping("/addUser")
    public void addUser(@RequestBody LoginUserDTO dto){
        UserDetails user = new User(dto.getUsername(),
                "{bcrypt}" + new BCryptPasswordEncoder().encode(dto.getPassword()), true,
                true,
                true,
                true,
                new ArrayList<>());
        manager.createUser(user);
    }
}
